package com.easyban.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.easyban.common.R;
import com.easyban.entity.UserAddress;
import com.easyban.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userAddress")
@Slf4j
public class UserAddressController {
    @Autowired
    UserAddressService userAddressService;

    @GetMapping("/{userId}")
    public R<List<UserAddress>> getUserAddress(@PathVariable String userId) {
        LambdaQueryWrapper<UserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAddress::getUserId, userId);
        List<UserAddress> list = userAddressService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping()
    public R<String> addUserAddress(@RequestBody UserAddress userAddress) {
        userAddress.setIsDefault(0);
        userAddressService.save(userAddress);

        return R.success("添加成功");
    }

    @DeleteMapping("/{id}")
    public R<String> deleteUserAddress(@PathVariable String id) {
        userAddressService.removeById(id);

        return R.success("删除成功");
    }
}
